<?php

namespace xcxApi\modules\guest\controllers;

use common\components\fast_api\Controller;
use common\datas\Enums;
use common\models\db\Banners;
use common\models\db\Files;
use xcxApi\models\db\Categories;

class PubController extends Controller
{
    /**
     * 这是一个测试
     * @param string $x
     * @return array
     */
    public function actionTest($x)
    {
        return $this->apiSuccess([
            'test' => $x,
        ]);
    }

    /**
     * 获取配置
     * @desc 获取配置
     * @return array
     * @return string versions.serverVersion 服务版本号
     */
    public function actionSettings()
    {
        return $this->apiSuccess([
            'versions' => \Yii::$app->params['versions'],
        ]);
    }

    /**
     * 获取所有根分类
     * @desc
     * @return array
     */
    public function actionCategoryRoots()
    {
        $categories = \Yii::$app->cache->getOrSet(APP_ID."-Guest-Pub-CategoryRoots", function () {
            $categories = Categories::find()
                ->andWhere(['status' => Enums::ACTIVE])
                ->andWhere(['pid' => null])
                ->orderBy(['cate_sort' => SORT_ASC])
                ->all();
            return $categories;
        }, 300);
//        $categories = Categories::find()
//            ->andWhere(['status' => Enums::ACTIVE])
//            ->all();
        return $this->apiSuccess(['list' => $categories]);
    }

    /**
     * 获取所有轮播图
     * @desc
     * @return array list
     */
    public function actionBannerList()
    {
        $banners = \Yii::$app->cache->getOrSet(APP_ID."-Guest-Pub-Banners", function () {
            $banners = Banners::find()
                ->andWhere(['status' => Enums::ACTIVE])
                ->all();
            $items = [];
            foreach ($banners as $k => $v) {
                $item = $v->toArray();
//                $item['url'] = Files::thumbImageUrl($v->url);
                $items[] = $item;
            }
            return $items;
        }, 300);
        return $this->apiSuccess(['list' => $banners]);
    }
}
